<template>
  <div class="container">
    <el-dialog
      title="新增标签"
      :visible="dialogTableVisible"
      width="400px"
      @close="btnCancel"
    >
      <el-form
        ref="refForm"
        label-width="80px"
        :model="formInfo"
        :rules="rules"
      >
        <el-form-item v-if="!queryInfo.id" label="所属学科" prop="subjectID">
          <el-select
            v-model="formInfo.subjectID"
            placeholder="请选择"
            style="width:100%"
          >
            <el-option
              v-for="(item, index) in sbjList"
              :key="index"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标签名称" prop="tagName">
          <el-input
            v-model="formInfo.tagName"
            autocomplete="off"
            placeholder="请输入标签名称"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="btnCancel">取消</el-button>
        <el-button type="primary" @click="btnOK">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { tagAdd } from '@/api/hmmm/tags.js'
export default {
  name: 'tagAdd',
  props: {
    dialogTableVisible: {
      type: Boolean,
      default: false
    },
    sbjList: {
      type: Array,
      require: true
    },
    queryInfo: {
      type: Object
    }
  },
  data() {
    return {
      formInfo: {
        subjectID: '',
        tagName: ''
      },
      rules: {
        subjectID: [
          { required: true, message: '请选择所属学科', trigger: 'blur' }
        ],
        tagName: [
          { required: true, message: '请输入标签名称', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    if (this.queryInfo.id) {
      // 如果是从其他页面来，直接隐藏选框，将路由中携带的id进行覆盖赋值
      this.formInfo.subjectID = this.queryInfo.id
    }
  },
  methods: {
    async btnOK() {
      try {
        // 验证表单规则
        await this.$refs.refForm.validate()
        await tagAdd(this.formInfo)
        this.$emit('addTagOK')
        this.$emit('update:dialog-table-visible', false)
        this.$message.success('添加成功')
      } catch (error) {
        this.$message.error('添加失败')
      }
    },
    btnCancel() {
      // this.formInfo = {
      //   subjectID: '',
      //   tagName: ''
      // }
      this.$refs.refForm.resetFields()
      this.$emit('update:dialog-table-visible', false)
    }
  }
}
</script>

<style scoped lang="less"></style>
